package com.sllp.C71S3TsllpShop.web.back;

import java.io.File;
import java.io.IOException;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.multipart.MultipartFile;

import com.sllp.C71S3TsllpShop.bean.Admin;
import com.sllp.C71S3TsllpShop.bean.Notice;
import com.sllp.C71S3TsllpShop.bean.Result;
import com.sllp.C71S3TsllpShop.biz.impl.NoticeBizImpl;

@Controller
public class BackNoticeWriteAction {

	@Resource
	private NoticeBizImpl nbi;
	
	@GetMapping("back/toNoticeWrite.do")
	public String toNoticeWrite() {
		return "back/notice-write::noticeWrite";
	}
	
	@ResponseBody
	@PostMapping("back/backNoticeAdd.do")
	public Result backNoticeAdd(Notice notice,@SessionAttribute("backLoginAdmin") Admin admin) {
		if(notice.getId()!=null) {
			// 修改公告
			Result result = nbi.updateNotice(notice);
			return result;
		}
		System.out.println(notice.getId());
		notice.setAdmin(admin.getId());
		notice.setState(2);
		System.out.println(notice);
		Result result = nbi.addNotice(notice);
		return result;
	}
	
	@Value("${spring.resources.staticLocations}")
	private String [] uploadDir;
	
	@PostMapping("back/uploadimg.do")
	@ResponseBody
	public String toUploadImg(String CKEditorFuncNum,@RequestParam("upload") MultipartFile file) throws IllegalStateException, IOException {
		String path= uploadDir[0].substring("file:/".length());
		String fileName = "notice/"+file.getOriginalFilename();
		String userFileName=file.getOriginalFilename();
		String suffix = userFileName.substring(userFileName.lastIndexOf('.'));
		String name=userFileName.substring(0, userFileName.lastIndexOf('.'));
		String addFileName=String.valueOf(System.currentTimeMillis()).substring(6);
		fileName = "notice/"+name+addFileName+suffix;
		
		String contentType = file.getContentType();
		String result="";
		if (contentType.equals("image/pjpeg")
				|| contentType.equals("image/jpeg")) {
			// IE6上传jpg图片的headimageContentType是image/pjpeg，而IE9以及火狐上传的jpg图片是image/jpeg
		} else if (contentType.equals("image/png")
				|| contentType.equals("image/x-png")) {
			// IE6上传的png图片的headimageContentType是"image/x-png"
		} else if (contentType.equals("image/gif")) {
		} else if (contentType.equals("image/bmp")) {
		} else {
			result="<script type=\"text/javascript\">"
					+"window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum
					+ ",''," + "'文件格式不正确（必须为.jpg/.gif/.bmp/.png文件）');"
					+"</script>";
			return result;
		}
		
		
		File diskFile=new File(path+fileName);
		if (!diskFile.exists()) { // 如果路径不存在，创建
			diskFile.mkdirs();
		}
		file.transferTo(diskFile);
		result="<script type=\"text/javascript\">"
				+ "window.parent.CKEDITOR.tools.callFunction("+CKEditorFuncNum+",'"+"/"+fileName+"')"
				+ "</script>";
		return result;
	}
	
}
